package com.learning.assignment_1;

import java.util.List;

import android.opengl.Visibility;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.learning.database.*;

public class LoginActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginpage);

		Button btnLogin = (Button) findViewById(R.id.main_btnLogin);
		final TextView status = (TextView) findViewById(R.id.main_status);
		final EditText username = (EditText) findViewById(R.id.main_username);
		final EditText password = (EditText) findViewById(R.id.main_password);

		btnLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (CheckLogin(username.getText().toString(), password.getText().toString()) == true) {
					try {
					Toast.makeText(getApplicationContext(),"Welcome " + username.getText(),Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(LoginActivity.this,
							MainActivity.class);
					startActivity(intent);
					finish();
					}
					catch(Exception e) {
						Integer x = 100;
						x+=1;
					}
					
				} else {
					status.setText("Username and password are missmatch!!!");
				}
			}
		});

		Button btnRegister = (Button) findViewById(R.id.main_btnRegister);
		btnRegister.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this,
						RegisterActivity.class);
				startActivity(intent);
			}
		});
		
		
		
		
	}

	public boolean CheckLogin(String Username, String Password) {
		DatabaseHelper db = DatabaseHelper.getInstance(this
				.getApplicationContext());
		return db.checkLogin(Username, Password);
	}
	
}
